<?php
/**
 * @copyright Copyright (C) 2010 redCOMPONENT.com. All rights reserved.
 * @license GNU/GPL, see license.txt or http://www.gnu.org/copyleft/gpl.html
 * Developed by email@recomponent.com - redCOMPONENT.com
 *
 * redSHOP can be downloaded from www.redcomponent.com
 * redSHOP is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License 2
 * as published by the Free Software Foundation.
 *
 * You should have received a copy of the GNU General Public License
 * along with redSHOP; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

class newslettersubscr_detailModelnewslettersubscr_detail extends JModel
{
	var $_id = null;
	var $_data = null;
	var $_table_prefix = null;

	function __construct()
	{
		parent::__construct();

		$this->_table_prefix = '#__redshop_';

		$array = JRequest::getVar('cid',  0, '', 'array');

		$this->setId((int)$array[0]);

	}
	function setId($id)
	{
		$this->_id		= $id;
		$this->_data	= null;
	}

	function &getData()
	{
		if ($this->_loadData())
		{

		}else  $this->_initData();

	   	return $this->_data;
	}

	function _loadData()
	{
		if (empty($this->_data))
		{
			$query = 'SELECT ns.*,uf.firstname FROM '.$this->_table_prefix.'newsletter_subscription as ns left join '.$this->_table_prefix.'users_info as uf on  ns.user_id = uf.user_id  WHERE ns.subscription_id = '. $this->_id;
			$this->_db->setQuery($query);
			$this->_data = $this->_db->loadObject();
			return (boolean) $this->_data;
		}
		return true;
	}


	function _initData()
	{
		if (empty($this->_data))
		{
			$detail = new stdClass();
			$detail->subscription_id	= 0;
			$detail->user_id			= 0;
			$detail->date				= null;
			$detail->newsletter_id		= null;
			$detail->name				= null;
			$detail->email				= null;
			$detail->published			= 1;
			$this->_data		 		= $detail;
			return (boolean) $this->_data;
		}
		return true;
	}
  	function store($data)
	{
		$row =& $this->getTable();

		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		if (!$row->store()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		return $row;
	}

	function delete($cid = array())
	{
		if (count( $cid ))
		{
			$cids = implode( ',', $cid );

			$query = 'DELETE FROM '.$this->_table_prefix.'newsletter_subscription WHERE subscription_id IN ( '.$cids.' )';
			$this->_db->setQuery( $query );
			if(!$this->_db->query()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}

		return true;
	}

	function publish($cid = array(), $publish = 1)
	{
		if (count( $cid ))
		{
			$cids = implode( ',', $cid );

			$query = 'UPDATE '.$this->_table_prefix.'newsletter_subscription'
				. ' SET published = ' . intval( $publish )
				. ' WHERE subscription_id IN ( '.$cids.' )';
			$this->_db->setQuery( $query );
			if (!$this->_db->query()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}

		return true;
	}

	function getuserlist()
	{
		$query = 'SELECT user_id as value,firstname as text FROM '.$this->_table_prefix.'users_info as rdu, #__users as u WHERE  rdu.user_id=u.id AND rdu.address_type LIKE "BT"';
		$this->_db->setQuery( $query );
		return $this->_db->loadObjectlist();
	}
	function getnewsletters()
	{
		$query = 'SELECT newsletter_id as value,name as text FROM '.$this->_table_prefix.'newsletter WHERE published=1';
		$this->_db->setQuery( $query );
		return $this->_db->loadObjectlist();
	}
	function getuserfullname2($uid)
	{
		$query = "SELECT firstname,lastname,username FROM ".$this->_table_prefix."users_info as uf LEFT JOIN #__users as u ON (uf.user_id=u.id) WHERE user_id='".$uid."' AND uf.address_type like 'BT'";
		$this->_db->setQuery($query);
		$this->_username = $this->_db->loadObject();

		if(count($this->_username)>0)
		{
			$fullname=$this->_username->firstname." ".$this->_username->lastname. ($this->_username->username !="" ? " (".$this->_username->username.")" : "");
		}
		else
		{
			$fullname = "";
		}
		return $fullname;
	}
	function getnewslettersbsc(){

		$query = 'SELECT ns.*,n.name'
			. ' FROM '.$this->_table_prefix.'newsletter_subscription as ns,'.$this->_table_prefix.'newsletter as n WHERE ns.newsletter_id=n.newsletter_id';
		$this->_db->setQuery($query);
		return $this->_db->loadObjectlist();
	}
	function getuserfullname($uid)
	{
		$query = "SELECT uf.firstname,uf.lastname,u.email FROM ".$this->_table_prefix."users_info as uf , #__users as u WHERE uf.user_id=".$uid." AND  uf.user_id = u.id and uf.address_type like 'BT'";
		$this->_db->setQuery($query);
		return $this->_db->loadObject();
		//$fullname=$this->_username->firstname." ".$this->_username->lastname;
		//return $fullname;
	}
	
	function getUserFromEmail($email)
	{
		$query = "SELECT * FROM ".$this->_table_prefix."users_info AS uf "
				."WHERE uf.address_type='BT' "
				."AND uf.user_email='".$email."' ";
		$this->_db->setQuery($query);
		$list = $this->_db->loadObject();
		return $list;
	}
}
?>